package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfSignNo;
import com.ocom.common.entity.typehandler.SignNoHandle;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfSignNo;
import com.ocom.payhandle.service.XfSignNoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class XfSignNoServiceImpl extends ServiceImpl<IDaoXfSignNo, EntityXfSignNo> implements XfSignNoService {

    @Autowired
    private IDaoXfSignNo iDaoXfSignNo;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntityXfSignNo selectSignNo(Long comId, Long merchId, Long pId,Integer payWay) {
        //查签约号
//        log.info("查签约号 【{}】【{}】【{}】",comId,merchId,pId);
        QueryWrapper<EntityXfSignNo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("sign_no","pay_way");
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",merchId);
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("status",1);
        if (payWay != null) {
            queryWrapper.eq("pay_way", payWay);
        } else {
//            queryWrapper.ne("pay_way", 32);
        }
        queryWrapper.orderByDesc("sort");
        queryWrapper.last(" limit 1");
        return this.getOne(queryWrapper);
    }

    //仅支付宝
    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public List<EntityXfSignNo> getSignList(Long comId,Integer payWay, List<Integer> courseIds) {
        //查签约号
//        log.info("查签约号 【{}】【{}】",comId,courseIds);
        QueryWrapper<EntityXfSignNo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("p_id","face_open_id");
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("pay_way",28);
        queryWrapper.groupBy("p_id");
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfSignNo getUserSignInfo(Long companyId, Long pId, Integer merchId, Integer payWay, Integer status) {
        QueryWrapper<EntityXfSignNo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("p_id", pId);
        if(merchId != null && merchId != -1){
            queryWrapper.eq("merch_id", merchId);
        }
        queryWrapper.eq("pay_way", payWay);
        if (null != status && -1 != status) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    public Boolean deleteBypId(long comId,  int merchId, long pId, int payWay, int status){
        UpdateWrapper<EntityXfSignNo> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", comId);
        updateWrapper.eq("merch_id", merchId);
        updateWrapper.eq("p_id", pId);
        updateWrapper.eq("pay_way", payWay);
        updateWrapper.eq("status", status);
        return this.remove(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfSignNo getUserSignInfo(Long companyId, Integer payWay, String signNo) {
        SignNoHandle signNoHandle = new SignNoHandle();
        String signNoStr = signNoHandle.encrypt(signNo, 1l);

        QueryWrapper<EntityXfSignNo> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("sign_no",signNoStr);
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("pay_way",payWay);
        queryWrapper.eq("status",1);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public int existSignNo(Long companyId, int merchId, long pId) {
        LambdaQueryWrapper<EntityXfSignNo> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(EntityXfSignNo::getComId,companyId);
        queryWrapper.eq(EntityXfSignNo::getMerchId,merchId);
        queryWrapper.eq(EntityXfSignNo::getPId,pId);
        queryWrapper.eq(EntityXfSignNo::getStatus,1);
        return this.count(queryWrapper);
    }
}
